package com.jiot.plugin.motan.client;

import com.jfinal.aop.AopFactory;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.validate.Validator;
import com.weibo.api.motan.config.ProtocolConfig;
import com.weibo.api.motan.config.RefererConfig;
import com.weibo.api.motan.config.RegistryConfig;
import rpc_service.MyRPC;
import java.lang.reflect.Field;

public class MyAopFactory extends AopFactory {

    private static final Log LOG = Log.getLog(MyAopFactory.class);

    
    
    
    
    @Override
    protected void doInject(Class<?> targetClass, Object targetObject) throws ReflectiveOperationException {
		targetClass = getUsefulClass(targetClass);
		Field[] fields = targetClass.getDeclaredFields();
		if (fields.length != 0) {
			for (Field field : fields) {
				Inject inject = field.getAnnotation(Inject.class);
				if (inject != null) {
					Class<?> fieldInjectedClass = inject.value();
					if (fieldInjectedClass == Void.class) {
						fieldInjectedClass = field.getType();
					}
					
					Object fieldInjectedObject = doGet(fieldInjectedClass);
					field.setAccessible(true);
					field.set(targetObject, fieldInjectedObject);
					continue;
				}
				
				MyRPC rpcInject = field.getAnnotation(MyRPC.class);
                if (rpcInject != null) {
                    doInjectRPC(targetObject, field, rpcInject);
                    continue;
                }
			}
		}
		
		// 是否对超类进行注入
		if (injectSuperClass) {
			Class<?> c = targetClass.getSuperclass();
			if (c != Controller.class && c != Object.class && c != Validator.class && c != Model.class && c != null) {
				doInject(c, targetObject);
			}
		}
	}
    
    
    /**
     * 注入 rpc service
     *
     * @param targetObject
     * @param field
     * @param rpcInject
     */
    private void doInjectRPC(Object targetObject, Field field, MyRPC rpcInject) {

        try {
//            JbootrpcReferenceConfig serviceConfig = new JbootrpcReferenceConfig(rpcInject);
            Class<?> fieldInjectedClass = field.getType();
//
//            Jbootrpc jbootrpc = JbootrpcManager.me().getJbootrpc();

        	if(!rpcInject.isLink()) {
    			return;
    		}
        	ProtocolConfig protocol = new ProtocolConfig();
            protocol.setId(rpcInject.protocolId());
            protocol.setName(rpcInject.protocolName());
            protocol.setDefault(rpcInject.protocolDefault());
        	
            RegistryConfig registry = new RegistryConfig();
            registry.setRegProtocol(rpcInject.registryRegProtocol());
            registry.setAddress(rpcInject.registryAddress());
            registry.setCheck(rpcInject.registryCheck()); 
            registry.setDefault(rpcInject.registryDefault());
            
            RefererConfig refererConfig = new RefererConfig();
            refererConfig.setRegistry(registry);
            refererConfig.setProtocol(protocol);
            refererConfig.setGroup(rpcInject.Group());
            refererConfig.setVersion(rpcInject.Version());
            refererConfig.setShareChannel(rpcInject.IsShareChannel());
            refererConfig.setRequestTimeout(rpcInject.RequestTimeout());
        	
            refererConfig.setInterface(fieldInjectedClass);//这个必须是接口
			Object obj = refererConfig.getRef();
			
//            Object fieldInjectedObject = serviceObtain(fieldInjectedClass, serviceConfig);

            
            
            field.setAccessible(true);
            field.set(targetObject, obj);

        } catch (Exception ex) {
            LOG.error("can not inject rpc service in " + targetObject.getClass() + " by config " + rpcInject, ex);
        }
    }
    

    
}
